package com.zhyz.bookstore.util;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * * @description: 数据库操作工具类
 * * @author:
 * * @create: 2022-11-01 13:55
 */
public class DBUtil implements Serializable {
    //核心类
    static QueryRunner queryRunner = new QueryRunner();

    static Connection conn = JDBCUtils.getconnection();
    /**
     * 增删改数据库
     * @param sql 传入sql语句
     * @param args 将多个参数变成数组传入
     * @return
     */
    	/*
    update:
    	insert/delete/update
     */
    public int update(String sql,Object...args){
        try {
            for (Object object : args) {
                System.out.println(object);
            }
            return (queryRunner.update(conn, sql, args));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return -1;
    }


    /**
     * 单表查询一列数据
     * @param clazz 实体类
     * @param sql 传入sql语句
     * @param args 将多个参数变成数组传入
     */
    public <T> T queryForOne(Class<T> clazz,String sql,Object... args){
        try {
            System.out.println(queryRunner.query(conn, sql, new BeanHandler<T>(clazz), args));
            return queryRunner.query(conn, sql, new BeanHandler<T>(clazz), args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 单表查询多条数据
     * @param sql 传入sql语句
     * @param clazz 传入bean.class
     * @param args 传入所需要的查询条件
     * @param <T>
     * @return
     */
    public <T> List<T> queryForList(Class<T> clazz,String sql,Object... args){
        try {
            return queryRunner.query(conn,sql,new BeanListHandler<T>(clazz),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 多表连接查询
     * @param sql 传入sql语句
     * @param mHandler
     * @return
     */
    public List<Map<String,Object>> ManyTableQuery(String sql, MapListHandler mHandler){
        try {
            return queryRunner.query(conn, sql, mHandler);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 查询一行一列数据
     * @param sql 传入sql语句
     * @param args 传入所需要的查询条件
     * @return
     */
    public Object queryForStringValue(String sql,Object... args){
        try {
            return queryRunner.query(conn,sql,new ScalarHandler(),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }


}
